$(function(){
	var lookImg = document.getElementById('lookImg');
	var bigImg = document.getElementById('bigImg');
	var bigBnimg = document.getElementById('bigBnimg');
	var bigImgControl = document.getElementById('bigImgControl');
	var findImges = $('.text').find('img');
	for(var i = 0; i<findImges.length; i++){
		findImges[i].setAttribute('name',i);
	}
	var widthVm = $(window).width();
	var heightVm = $(window).height()*0.9;
	var imgHrml = '';
	var naumerImg;
	var numbersaide,numberCont;
	var oneFor = 0,twoFor = 0,numy = 0,numx = 0,bie = 0,set = 1,nume = 0;
	var starx,stary,movex,movey,chax,judgedx,judgedy,sumx,cheng; 
	
	function leftdong(sum){
		bigBnimg.style.transition="-webkit-transform 0.2s linear";
		bigBnimg.style.webkitTransform="translate3d("+sum+"px,0px,0px)";
	};
	
	var listAll,bigMleft,bigMwidth,hgtImg,widImg,scle = 1;
	var sx,sy,sz,mx,my,mz,fx,fy,maxHt,maxWd,ulWid;	
	
		numbersaide=0;
		if(oneFor == 0){
			oneFor = 1;
			for(var i = 0; i<findImges.length; i++){	
				imgHrml += '<li>'+
								'<div>'+
								'<img name="'+ i +'" src="'+ (findImges[i].getAttribute('data-src') || findImges[i].src) +'" width="100%"/>'+
								'</div>'+
						   '</li>';
				naumerImg = (i+1)*100;
			};
			$('#bigBnimg li').css('width',widthVm+'px')
			$('#bigBnimg').css('width',naumerImg +'%')
			$('#bigBnimg').html(imgHrml);
			$('#bigBnimg li').css('width',widthVm+'px');
			listAll = bigBnimg.getElementsByTagName('img');	
		}	

		$('#bigBnimg').css('left',-numbersaide*widthVm + 'px');
		numberCont = numbersaide + 1;
	
		$('#bigImgControl .rightNumer').html(numberCont+'/'+'<span>'+findImges.length+'</span>');
		$('.neirong .biaoti .right').html(numberCont+'/'+'<span>'+findImges.length+'</span>');
		
		listAll[numbersaide].addEventListener('touchstart',loike,false);
		function loike(){
				if(event.targetTouches.length == 1){
					bie = 1;
					bigMleft = bigBnimg.offsetLeft;
					bigMwidth = $('#bigBnimg').width() - widthVm;
					starx = event.targetTouches[0].pageX;
					stary = event.targetTouches[0].pageY;
					hgtImg = this.offsetHeight;
					fx = widthVm*scle;
					fy = hgtImg*scle;
					maxHt = (fy - heightVm)/2;
					maxWd = (fx - widthVm)/2;
				}else if(event.targetTouches.length == 2 && nume == 0){
					bie = 0;
					sx = event.targetTouches[0].pageX - event.targetTouches[1].pageX;
					sy = event.targetTouches[0].pageY - event.targetTouches[1].pageY;
					sz = parseInt(Math.sqrt(sx*sx + sy*sy));
				}
				this.addEventListener('touchmove',function(event){
					event.preventDefault();
					if(event.targetTouches.length == 1 && bie == 1){
						nume = 1;
						movex = event.targetTouches[0].pageX;
						movey = event.targetTouches[0].pageY;
						chax = movex - starx + bigMleft;
						judgedx = movex - starx + numx;
						judgedy = movey - stary + numy;
						if(fx > widthVm){	
							if(judgedx > maxWd){
								this.style.left = maxWd + 'px';
								widImg = judgedx - maxWd;
								bigBnimg.style.left = judgedx - maxWd + bigMleft + 'px';
								judgedx = maxWd;
							}else if(judgedx < -maxWd){
								this.style.left = -maxWd + 'px';
								widImg = judgedx + maxWd;
								bigBnimg.style.left = judgedx + maxWd + bigMleft + 'px';
								judgedx = -maxWd;
							}else{
								this.style.left = judgedx + 'px';
							};
						}else{
							widImg = movex - starx;
							bigBnimg.style.left = chax + 'px';
						};
						if(fy > heightVm && scle !== 1){
							if(judgedy > maxHt){
								this.style.top = maxHt + 'px';
								judgedy = maxHt;
							}else if(judgedy < -maxHt){
								this.style.top = -maxHt + 'px';
								judgedy = -maxHt;
							}else{
								this.style.top = judgedy + 'px';
							};
						}else if(fy > heightVm ){
							if(judgedy > 0 ){
								this.style.top = 0 + 'px';
								judgedy = 0;
							}else if(judgedy < -maxHt*2){
								this.style.top = -maxHt*2 + 'px';
								judgedy = -maxHt*2;
							}else{
								this.style.top = judgedy + 'px';
							};
						}
					}else if(event.targetTouches.length == 2 && nume == 0){
						mx = event.targetTouches[0].pageX - event.targetTouches[1].pageX;
						my = event.targetTouches[0].pageY - event.targetTouches[1].pageY;
						mz = parseInt(Math.sqrt(mx*mx + my*my));
						scle = (mz - sz)/100 + set;
						if(scle>2){
							scle = 2;
						}else if(scle<=1){
							scle = 1;
							this.style.top = 0 +'px';
							this.style.left = 0 + 'px';
						};
						this.style.webkitTransform = "scale(" + scle + ")" + " translateZ(0)";
					}
				},false);
				this.addEventListener('touchend',function(){
					if(bie == 1 && event.touches.length == 0){
						nume = 0;
						bigMleft = bigBnimg.offsetLeft;
						numy = judgedy;
						numx = judgedx;
						if(widImg < -60){
							numbersaide += 1;
								judgedx = 0;	
								widImg = 0;
							if(numbersaide > findImges.length - 1){
								numbersaide = findImges.length - 1;
							}else{
								this.style.webkitTransform = "scale(1)";
								this.style.top = 0 + 'px';
								this.style.left = 0 + 'px';
								scle = 1;
								set = 0;
								numy = 0;
								numx = 0;
							}
						
						}else if(widImg > 60){
							numbersaide -= 1;
							judgedx = 0;
							widImg = 0;
							if(numbersaide < 0 ){
								numbersaide = 0;
							}else{
								this.style.webkitTransform = "scale(1)";
								this.style.top = 0 +'px';
								this.style.left = 0 + 'px';
								scle = 1;
								set = 0;
								numy = 0;
								numx = 0;
							}
						}else{
							numbersaide += 0;
						} 
						$('#bigImgControl .rightNumer').html(numbersaide+1+'/'+'<span>'+findImges.length+'</span>');
						
						for(var i = 0; i<document.getElementsByClassName("neirong").length; i++){	
							if(i==numbersaide){
								document.getElementsByClassName("neirong")[i].style.display="block";
								$('.neirong .biaoti .right').html(numbersaide+1+'/'+'<span>'+findImges.length+'</span>');
							}else{
								document.getElementsByClassName("neirong")[i].style.display="none";
							}
						}
						
						sumx = bigMleft+widthVm*numbersaide;
						leftdong(-sumx);
						if(Number(this.name) !== numbersaide){
							listAll[numbersaide].addEventListener('touchstart',loike,false);
							bigMleft = 0;
							this.removeEventListener('touchstart',loike,false);
						}
					}else if(bie == 0){
						
						sx = 0;
						sy = 0;
						mx = 0;
						my = 0;
						set = scle;
					};
					
				},false);
			}
			
			$('#bigBnimg img').click(function(e){
				$(".neirongbox").toggle();
				$(".rightNumer").toggle();
				$(".leftImg").toggle();
				$(".bottom_bars").toggle();

				var u = navigator.userAgent, app = navigator.appVersion;
				var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
				var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
				if (isAndroid) {
					window.newsControl.tapImage();
				} else if(isiOS) {
					tapImage();
				}
			});
	});
